﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FinMath
{
    [TestClass]
    public class UtilTest
    {
        [TestMethod]
        public void MathUtil_Stdev_Test()
        {
            double[] x = new double[] { 4, 0.3, 6 };
            double[] y = new double[] { 1, 1.2, 3 };

            Assert.AreEqual(2.8919428, Math.Round(FMath.Stdev(x), 7, MidpointRounding.AwayFromZero));
            Assert.AreEqual(1.1015141, Math.Round(FMath.Stdev(y), 7, MidpointRounding.AwayFromZero));
            Assert.AreEqual(1.5022222, Math.Round(FMath.Covar(x, y), 7, MidpointRounding.AwayFromZero));
            Assert.AreEqual(0.7073684, Math.Round(FMath.Correl(x, y), 7, MidpointRounding.AwayFromZero));
        }

        [TestMethod]
        public void FinanceUtil_PV_Test()
        {
            Assert.AreEqual(909.42, Math.Round(Finance.PV(0.0456, 30, 40, 1000), 2, MidpointRounding.AwayFromZero));
            Assert.AreEqual(646.98, Math.Round(Finance.PV(0.0456, 30, 40), 2, MidpointRounding.AwayFromZero));

            Assert.AreEqual(6275.61, Math.Round(Finance.FV(0.0456, 30, 40, 1000), 2, MidpointRounding.AwayFromZero));
            Assert.AreEqual(2465.24, Math.Round(Finance.FV(0.0456, 30, 40), 2, MidpointRounding.AwayFromZero));

            Assert.AreEqual(63.45, Math.Round(Finance.PMT(0.0456, 30, 1000, 100), 2, MidpointRounding.AwayFromZero));
            Assert.AreEqual(61.83, Math.Round(Finance.PMT(0.0456, 30, 1000), 2, MidpointRounding.AwayFromZero));
        }
    }
}
